package com.jms.login.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jms.login.dao.CustomerDao;
import com.jms.login.po.Customer;

@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
	/**
	 * 通过Spring引入JPA需要的entityManage对象
	 */
	@Autowired(required=true)
	@PersistenceContext(name="unitName")
	private EntityManager entityManager;
	
	@Override
	public Customer findByUserNameAndPassWord(Customer customer) {
		String jpa_sql = "select customer from com.jms.login.po.Customer customer "
						+ "where customer.username=:username and customer.password=:password";
		List<Customer> customer_list = entityManager.createQuery(jpa_sql)
				.setParameter("username", customer.getUsername())
				.setParameter("password", customer.getPassword())
				.getResultList();
		if (customer_list.isEmpty()) {
			entityManager.close();
			return null;
		} else {
			Customer customer_find = customer_list.get(0);
			entityManager.close();
			System.out.println("Dao:"+customer_find);
			return customer_find;
		}
	}

}
